import string

def indent(s, num_spaces):
    """Return string s with each line indented by num_spaces spaces."""
    indented = [ "%s%s" % (num_spaces*' ', line) for line in s.strip('\n').split('\n') ]
    return '\n'.join(indented)
    
#def strip_blank_lines(s):
    #stripped = ''
    #for line in s.split('\n'):
        #if line: stripped = stripped + line + '\n'
    #return stripped

def strip_blank_lines(s):
    stripped = [line for line in s.split('\n') if not (line.isspace() or line=='')]
    return '\n'.join(stripped)
    
def strip_multiple_blank_lines(s):
    """If two or more blank lines follow each other, make them just a single blank line."""
    lines = s.split('\n')
    stripped = []
    for i in range(len(lines)-1):
        current = lines[i]
        next = lines[i+1]
        if (current.isspace() or current=='') and (next.isspace() or next==''):
            pass
        else:
            stripped.append(current)
    last = lines[-1]
    if not (last.isspace() or last[-1]==''):
        stripped.append(last)
    return '\n'.join(stripped)

def strip_comment_lines(s, c='#'):
    lines = s.split('\n')
    stripped = []
    for line in lines:
        if not line[0] == c:
            stripped.append(line)
    return '\n'.join(stripped)
    

def space2underscore(s):
    return s.replace(' ','_')
    
def underscore2space(s):
    return s.replace('_',' ')

def sfilter(s):
    if ' ' in s:
        return space2underscore(s)
    if '_' in s:
        return underscore2space(s)
    else:
        return s
    
def max_length(list_of_strings):
    """Return the length of the longest string in a list of strings."""
    longest = 0
    for s in list_of_strings:
        length = len(s)
        if length > longest:
            longest = length
    return longest

def max_line(multi_line_string):
    """Return the length of the longest line in a multi line string."""
    longest = 0
    lines = multi_line_string.split('\n')
    for l in lines:
        length = len(l)
        if length > longest:
            longest = length
    return longest

def string_summary(s, threshold=100, edgelines=3, sum_symbol='...'):
    lines = s.split('\n')
    if len(lines) > threshold:
        summary = lines[:edgelines] + [sum_symbol] + lines[-edgelines:]
        return '\n'.join(summary)
    else:
        return s
    
def two_columns(string1, string2, num_spaces=2, sep_symbol=None):
    """Return the two strings in two columns, separated by num_spaces and the sep_symbol if provided."""
    if sep_symbol:
        separation = num_spaces/2 * ' ' + sep_symbol + num_spaces/2 * ' '
    else:
        separation = num_spaces * ' '
    lines1 = string1.split('\n')
    if not lines1[-1]:
        lines1.pop()
    lines2 = string2.split('\n')
    if not lines2[-1]:
        lines2.pop()
    number_lines1 = len(lines1)
    number_lines2 = len(lines2)
    width1 = max_length(lines1)
    s = ""
    for i in range(max(number_lines1,number_lines2)):
        if i > number_lines1 - 1:
            column1 = " "
            column2 = lines2[i]
        elif i > number_lines2 - 1:
            column1 = lines1[i]
            column2 = " "
        else:
            column1 = lines1[i]
            column2 = lines2[i]
        s += string.ljust(column1, width1) + separation + column2 + '\n'
    return s

def columns(list_of_strings, num_spaces=2, sep_symbol=None):
    """Return the strings in the list in columns, separated by num_spaces and the sep_symbol if provided."""
    if len(list_of_strings) == 1:
        return str( list_of_strings[0] )
    elif len(list_of_strings) == 2:
        return two_columns(list_of_strings[0], list_of_strings[1], num_spaces, sep_symbol)
    return two_columns(list_of_strings[0], columns(list_of_strings[1:], num_spaces, sep_symbol), num_spaces, sep_symbol)

def stripprefix(s, prefix):
    if s.startswith(prefix):
        return s[len(prefix):]
